/**
 * controller_activity.js
 * 活动相关控制器
 * 
 * @author  王中艺 <wangzy_smile@qq.com>
 * @date    2017-05-12
 */

//活动首页
app.controller('ActivityIndex', function($scope, $location, $http, $sce, common, wechat){
    //变量声明
    $scope.nActivity    = {};
    $scope.activityList = [];
    $scope.leaveMsgList = [];
    $scope.joinCount    = 0;

    //设置网站标题
    common.setTitle('主题活动');
    //微信分享设置(隐藏菜单)
    wechat.wxConfig();

    //加载数据
    $scope.load_data    = function(){
        var url     = config.prefix_url + '/Activity/index?code='+config.code;

        $http.get(url).success(function(info){
            // console.log(info);
            if(info.code != 1){
                common.dealError(info);
                return false;
            }

            $scope.nActivity    = info.data.activity_info;
            $scope.activityList = info.data.activity_list;
            $scope.joinCount    = info.data.join_count;
            $scope.leaveMsgList = info.data.leavemsg_list;

            common.trustAsHtml('activity', $scope.nActivity, false);
        })
    }

    //购买会员
    $scope.purchase_vip     = function(){
        common.logAnchor();
        $location.path('/Vip/purchase');
    }

    //邀请好友
    $scope.share    = function(){
        $location.path('/User/2dcode');
    }

    //活动详情
    $scope.get_item     = function(activityId){
        S('activity_id', activityId);
        $location.path('/Activity/get');
    }

    //写留言
    $scope.write_leavemsg   = function(){
        common.writeLeaveMsg($scope.nActivity.title, {
            'relate_type'   : 2,
            'relate_id' : $scope.nActivity.id
        })
    }

    $scope.load_data();
})

//活动详情
app.controller('ActivityGet', function($scope, $location, $http, $timeout, $anchorScroll, common, wechat){
    // $scope.activityId    = S('activity_id');
    $scope.nActivity    = {};
    $scope.leaveMsgList = [];
    $scope.initKnownAudio   = false;
    $scope.nTab     = 1;
    $scope.pictureList  = new Array();
    $scope.userInfo     = new Object();
    $scope.picture  = {
        'parent'    : [],
        'children'  : []
    }

    // if(!$scope.activityId){
    //  common.doAlert('活动ID无效!');
    //  return false;
    // }

    //初始化JS
    $scope.init_js  = function(){
        common.setTitle('活动详情');
        wechat.wxConfig();

        //切页监听
        $('.am-nav-tabs').find('a').on('open.tabs.amui', function(e){
            if($(this).html() == '家长须知' && !$scope.initKnownAudio){
                $scope.init_known_audio($scope.nActivity.known_list);
            }

            switch($(this).html()){
                case '活动方案':
                    $scope.$apply(function(){$scope.nTab    = 1;});
                break;
                case '家长须知':
                    $scope.$apply(function(){$scope.nTab    = 2;});
                    if(!$scope.initKnownAudio){
                        $scope.init_known_audio($scope.nActivity.known_list);
                    }
                break;
                case '制作相册':
                    $scope.$apply(function(){$scope.nTab    = 3;});
                    if($scope.pictureList !== null){
                        var param   = {
                            'user_id'   : $scope.userInfo.id,
                            'activity_id'   : $scope.nActivity.id
                        }
                        param   = base64encode(objectToHttp(param));
                        var url   = 'index.html?param='+param+'&#/Activity/picture';
                        window.open(url, '_self');
                    }
                break;
            }
        })

        $timeout(function(){
            //上传插件声明
            $('#upload-picture-children,#upload-picture-parent').uploadifive({
                'auto'              : true,
                'buttonClass'       : 'btn-upload-picture',
                'uploadScript'      : config.prefix_url+'/Public/upload',
                'removeCompleted'   : true,
                'onUploadComplete'  : function(file, info) {
                    var info    = eval('('+info+')');
                    var domId   = $(this).attr('id');
                    var file    = info.data.file;
                    var fileExt     = file.info.ext.toLowerCase();

                    if(!common.inArray(fileExt, ['jpg'])){
                        common.doAlert('文件格式只能为jpg!');
                        return false;
                    }
                    $scope.$apply(function(){
                        switch(domId){
                            case 'upload-picture-children':
                                $scope.picture.children.push(file.src);
                            break;
                            case 'upload-picture-parent':
                                $scope.picture.parent.push(file.src);
                            break;
                        }
                    })
                }
            });
        }, 300);    
    }

    //加载数据
    $scope.load_data    = function(){
        // let url  = config.prefix_url + '/Activity/getItem?activity_id='+$scope.activityId;
        var url     = config.prefix_url + '/Activity/getNewItem?code='+config.code;

        $http.get(url).success(function(info){
            console.log(info);
            if(info.code != 1){
                common.dealError(info);
                return false;
            }

            $scope.nActivity    = info.data.activity;
            $scope.leaveMsgList = info.data.leavemsg_list;
            $scope.pictureList  = info.data.picture_list;
            $scope.userInfo     = info.data.user_info;

            common.trustAsHtml('activity', $scope.nActivity, false);
            //初始化音频
            var audioInfo   = {
                'title' : $scope.nActivity.title,
                'author': '专家解读',
                'audio' : $scope.nActivity.audio
            }
            common.initAudio(document.getElementById('audio'), audioInfo);

            var iAudioInfo  = {
                'title' : $scope.nActivity.title,
                'author': '活动介绍',
                'audio' : $scope.nActivity.i_audio
            }
            common.initAudio(document.getElementById('i-audio'), iAudioInfo);
        })
    }

    //写留言
    $scope.write_leavemsg   = function(){
        common.writeLeaveMsg($scope.nActivity.title, {
            'relate_type'   : 2,
            'relate_id' : $scope.nActivity.id
        })
    }

    //初始化用户须知音频
    $scope.init_known_audio     = function(knownList){
        $scope.initKnownAudio   = true;
        var init_audio  = function(audio){
            audio.aplayer   = common.initAudio(
                document.getElementById('known-audio-'+audio['id']), {
                    'title' : '用户须知声音',
                    'author'    : '欣乐土',
                    'audio' : audio['audio']
                }
            )
            audio.aplayer.on('play', function(){
                $scope.$apply(function(){
                    audio.is_play   = true;
                })
            })
            audio.aplayer.on('pause', function(){
                $scope.$apply(function(){
                    audio.is_play   = false;
                })
            })
        }
        for(var i in knownList){
            init_audio(knownList[i]);
        }
    }

    //播放用户须知声音
    $scope.toggle_aplayer   = function(item){
        item.aplayer.toggle();
    }

    //跳至锚点
    $scope.goto_anchor  = function(domId){
        $(window).scrollTo(domId, 500);
    }

    //上传图片
    $scope.upload_picture   = function(){
        var url     = config.prefix_url + '/Activity/addPicture';
        if($scope.picture.parent.length == 0){
            common.doAlert('请上传父母拍的照片!');
            return false;
        }
        if($scope.picture.children == 0){
            common.doAlert('请上传宝宝拍的照片!');
            return false;
        }
        var param   = {
            'activity_id'   : $scope.nActivity.id,
            'parent_pics'   : $scope.picture.parent,
            'child_pics'    : $scope.picture.children
        }
        console.log(param);
        $http.post(url, param).success(function(info){
            console.log(info);
            if(info.code != 1){
                common.dealError(info);
                return false;
            }

            common.doAlert('相册生成成功!');
            window.history.go(0);
        })
    }

    $scope.load_data();
    $scope.init_js();
})

//活动列表
app.controller('ActivityList', function($scope, $location, $http, $timeout, common, wechat){
    //变量声明
    $scope.list     = new Array();
    $scope.nPage    = 1;
    $scope.userInfo     = new Object();

    common.setTitle('活动列表');
    wechat.wxConfig();

    //加载数据
    $scope.get_list     = function(page){
        var page    = page ? page : 1;
        var url     = config.prefix_url + '/Activity/getList?p='+page;
        $http.get(url).success(function(info){
            console.log(info);
            if(info.code != 1){
                common.dealError(info);
                return false;
            }

            $scope.nPage    = page;
            $scope.list     = info.data.list;
            $scope.userInfo     = info.data.user_info;
        })
    }

    //相册查看
    $scope.scan_picture     = function(item){
        var param   = {
            'activity_id'   : item.id,
            'user_id'   : $scope.userInfo.id
        }

        param   = base64encode(objectToHttp(param));
        var url   = 'index.html?param='+param+'&#/Activity/picture';
        window.open(url, '_self');
    }

    $scope.get_list(1);
})

// //活动相册
// app.controller('ActivityPicture', function($scope, $location, $http, $timeout, common, wechat){
//  $scope.list     = new Array();
//  $scope.activityInfo     = S('activity_info');

//  if(!$scope.activityInfo){
//      common.doAlert('活动信息获取失败!');
//      $location.path('/Activity/list');
//      return false;
//  }

//     //初始化JS
//     $scope.init_js   = function(){
//      common.setTitle('主题相册');
//      wechat.wxConfig();
//     }

//  $scope.get_list     = function(){
//      var url     = config.prefix_url + '/Activity/getPictureList?activity_id='+$scope.activityInfo.id;
//      $http.get(url).success(function(info){
//          console.log(info);
//          if(info.code != 1){
//              common.dealError(info);
//              return false;
//          }

//          $scope.list     = info.data.list;
//          $timeout(function(){
//              $('.am-slider').flexslider();
//          }, 500);
//      })
//  }

//  $scope.get_list();
//  $scope.init_js();
// })